package toy.keli.edic.ui.main.components

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import toy.keli.edic.ui.main.components.Display.px2dp
import kotlin.math.absoluteValue

@Composable
@Preview(showBackground = true)
fun DotPage(
    size:Int=3,                     //页数
    modifier:Modifier=Modifier.fillMaxSize(),
    onChagePage:(Int)->Unit={},     //修改页面事件 传入的是页面页数
    interval:Dp = 5.dp,             //点的间距
    breadth:Dp = 5.dp,              //点的宽度
    defColor:Color = Color.Gray,    //默认颜色
    selColor:Color = Color.Blue,    //当前项的颜色
    content:@Composable ()->Unit={},        //拖拽效果的子元素
){
    val index = remember { mutableStateOf(0) }
    val offsetX = remember { mutableStateOf(0f) }

    Box (modifier = modifier,
        contentAlignment = Alignment.BottomEnd
    ) {
        Row(modifier = Modifier
            .fillMaxWidth()
            .alpha(0.8f),
            horizontalArrangement = Arrangement.Center
        ) {
            LazyRow {
                items(size) { i ->
                    var size = if (index.value == i) breadth*2 else breadth
                    var color = if (index.value == i) selColor else defColor
                    Column(modifier = Modifier.padding(end = interval)) {
                        Text(
                            text = "", modifier = Modifier
                                .background(color, shape = CircleShape)
                                .height(breadth * 2)
                                .padding(horizontal = size)
                        )
                    }
                }
            }
        }
        Column (modifier=Modifier
            .fillMaxSize()
            .padding(bottom = 20.dp)
//            .offset(x= px2dp(offsetX.value.toInt()))
            .draggable(
                state = rememberDraggableState(onDelta = {}),
                Orientation.Horizontal,
                //开始拖拽
                onDragStarted = { offset ->
                    offsetX.value += offset.x
                    Log.d( "DotPage: ","x:"+offsetX.value + " ox:"+offset.x)
                },
                //拖拽结束
                onDragStopped = {
                    if (it.absoluteValue > 10) {
                        var newIndex = if (it < 0) index.value + 1 else index.value - 1
                        if (newIndex >= 0 && newIndex < size) {
                            index.value = newIndex
                            onChagePage(index.value)
                        }
                    }
                    offsetX.value = 0f
                }
            )
        ){
//            LazyColumn {
//                items(150){
//                    Text("aaaaa")
//                }
//            }
            content()
        }
    }
}